#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
using namespace std;

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        for (auto e : nums)
        {
            sum += e;
        }
        int target = sum - x;
        if (target < 0)
            return -1;
        int rls = 0;
        int rll = -1;
        for (int left = 0, right = 0; right < nums.size(); ++right)
        {
            rls += nums[right];
            while (rls > target) rls -= nums[left++];
            if (rls == target)
                rll = right - left + 1 > rll ? right - left + 1 : rll;
        }
        if (rll < 0)
            return -1;
        else
            return nums.size() - rll;
    }
};